// pages/shop/shop.js
var app = getApp();
var that;
Page({ 
  data: {
    success:0,  //支付成功后返回首页
    ordersn: '',  //支付成功的订单号
    imgUrls:[],
    categorys:[],
    productData:[],
    pages:1,  //获取第几页的数据
    shuaxin:true,
    keyword:'',
    no_pro:false,  //是否搜索到商品
    open:"", //点击过快设置的openid
    i:0,
    search_ss:"block",
    search_input:"none",
    search_animationData: {},
    recommend_shop:'', //推荐的商品
    shopIndex: [],
    objectShop: []   
  },
  onLoad: function (options) {
    
   

    that = this;
    that.get_shop_msg(); //获取店铺信息
    app.getUserInfo(function (res) {
      that.setData({ open: res.openid });
    }, function () {
      that.onLoad(options)
    });
    //that.setData({ open: "1" });
    that.get_shop(); //获取商品信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          h: res.windowHeight
        });
      }
    }); 

    this.setData({
      index: wx.getStorageSync('shop_index')
    });

  },
  onShow: function () {
    console.info(wx.getStorageSync('shop_id'));
   

    app.check_vs(); //检查版本
    that.get_recommend_shop();  //获取推荐商品
  
    // that.setData({mengcen:true});
    // wx.showToast({
    //   title: "商城非测试，直接连微擎后台，不可改动！！！",
    //   image: '../../images/g.png',
    //   duration: 99999
    // })
// that.setData({ keyword: '', pages: 1, productData: [],shuaxin: true, no_pro: false,});
//   that.get_shop();//每次加载重新获取商品

  },
  onHide: function () {   //每次隐藏之前重新获取一下
    // that.setData({ keyword: '', pages: 1, productData: [], shuaxin: true, no_pro: false, });
    // that.get_shop();
  },
  inputchange:function(e){
    // console.log(e.detail.value)
    that.setData({ keyword: e.detail.value.trim(), pages: 1})
  },
 
  // goods_detail:function(){
  //   wx.navigateTo({
  //     url: './goods_detail'
  //   })
  // },
  select_shop:function(){
    //if (that.data.search_ss=="block"){
      //that.search_start();
    //}else{
      //if (that.data.keyword != "") {
        wx.navigateTo({
          //url: './shop_search?keyword=' + that.data.keyword
          url: './shop_search'
        })
        //that.setData({ pages: 1 })
        //that.get_shop()
      //} else {
        //app.toast_up("无搜索内容!");
      //}
    //}
  },
  bindPickerChange: function (e) {
    that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var shop_id = that.data.shopIndex[e.detail.value].id;
    wx.setStorageSync('shop_id', shop_id);
    this.setData({
      index: e.detail.value
    });
    wx.setStorageSync('shop_index', e.detail.value);

    wx.reLaunch({
      url: 'shop'
    })
  },

  get_shop_msg: function () {

    app.req("get_shop.html", {}, function (res) {
      var shop_arr = [];
      if (res.status == "success") {
        for (var shop in res.mess) {
          shop_arr.push(res.mess[shop]);

        }

        that.setData({ shopIndex: shop_arr });
        that.setData({ objectShop: res.mess });
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示1", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_service_msg()
          }
        })
      }
    })
  },
  
  onPullDownRefresh: function () {
    that.setData({ keyword: '', pages: 1, shuaxin: true, no_pro: false, });
    wx.showNavigationBarLoading() //在标题栏中显示加载
    setTimeout(function () {
      that.setData({productData: []});
      that.get_shop(); //下拉刷新
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1200);
  },
  search_start:function(){
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })

    that.animation = animation

    animation.width("690rpx").step()

    that.setData({
      search_ss:"none",
      search_animationData: animation.export()
    })
    setTimeout(function(){
      that.setData({
        search_input: "block",
      })
    },500);
  
  },
  get_shop:function(){
    var shop_id = wx.getStorageSync('shop_id');
    app.req(app.local_url + "do=shop&p=index&op=goods", { "keyword": that.data.keyword, "page": that.data.pages, "od": that.data.openid,"type":"index","shop_id":shop_id }, function (res) {
      console.log(res)
      if (res.status == 1) {

        if (that.data.productData.length==0 && res.result.goods == '') { 
          that.setData({ productData:that.data.recommend_shop, no_pro: true }); return;
        } else { that.setData({ no_pro: false }); }
        
      
        if (res.result.goods == '') { app.toast_up("无更多数据");return}//商品加载完了
      
       that.setData({ productData: res.result.goods, pages: that.data.pages + 1, imgUrls: res.result.advs, categorys: res.result.categorys})

       setTimeout(function () { that.setData({ shuaxin: true }) }, 500)
        
      } else if (res.status ==0) {
        app.toast_up(res.result)
      } else {
        app.modal("错误提示2", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_shop()
          }
        })
      }
    })
  },
 /* onReachBottom: function () {
    console.log("滚到底了" + that.data.shuaxin);
    if (that.data.shuaxin) {
      that.setData({ shuaxin: false })
      that.get_shop();
    }
  },
  tobottom:function(){
    console.log(190)
      if (that.data.shuaxin){
        that.setData({ shuaxin: false }) 
        that.get_shop();     
      }
  },*/
  back_f:function(){that.setData({ success: 0 }) } ,//点击返回首页时隐藏
 
  delete_put: function () {
    this.setData({ keyword: "" });
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })

    that.animation = animation

    animation.width("169rpx").step()

    that.setData({
      search_input: "none",
      search_ss: "block",
      search_animationData: animation.export()
    })
  
  },
  get_recommend_shop:function(){  //推荐的商品
    var shop_id = wx.getStorageSync('shop_id');
    //console.info('shop_id:'+shop_id);
    app.req(app.local_url + "do=shop&p=goods_rank", { "od": app.data.openid,"shop_id":shop_id }, function (res) {
      // console.log(res)
      if (res.status == 1) {
        that.setData({ recommend_shop: res.result }); 
      } else if (res.status == 0) {
        app.toast_up(res.result)
      } else {
        app.modal("错误提示3", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_recommend()
          }
        })
      }
    })
  },
  img_lianjie:function(e){
    // console.log(e.currentTarget.dataset.id)
    if (e.currentTarget.dataset.id!=''){
      wx.navigateTo({
        url: './goods_detail?productId=' + e.currentTarget.dataset.id
      });
    }
  }
})